What is vscode-jsonrpc?
The vscode-jsonrpc package provides a framework for implementing JSON-RPC based communication. It is primarily used for building language servers and other tools that need to communicate over JSON-RPC.
What are vscode-jsonrpc's main functionalities?
Creating a JSON-RPC connection
This code demonstrates how to create a JSON-RPC connection using the vscode-jsonrpc package. It spawns a child process and sets up a message connection using the standard input and output streams of the child process.
const { createMessageConnection, StreamMessageReader, StreamMessageWriter } = require('vscode-jsonrpc');
const { spawn } = require('child_process');
const childProcess = spawn('node', ['path/to/your/server.js']);
const connection = createMessageConnection(
new StreamMessageReader(childProcess.stdout),
new StreamMessageWriter(childProcess.stdin)
);
connection.listen();
Sending a request
This code demonstrates how to send a request over a JSON-RPC connection. The sendRequest method sends a request with a specified method and parameters, and returns a promise that resolves with the response.
connection.sendRequest('example/request', { param1: 'value1' }).then(response => {
console.log('Received response:', response);
});
Handling notifications
This code demonstrates how to handle notifications over a JSON-RPC connection. The onNotification method registers a handler for notifications with a specified method.
connection.onNotification('example/notification', params => {
console.log('Received notification:', params);
});
Handling requests
This code demonstrates how to handle requests over a JSON-RPC connection. The onRequest method registers a handler for requests with a specified method, and the handler returns a response.
connection.onRequest('example/request', params => {
return { result: 'response' };
});
Other packages similar to vscode-jsonrpc
json-rpc2
The json-rpc2 package is a JSON-RPC 2.0 implementation for Node.js. It provides similar functionality to vscode-jsonrpc, including creating servers and clients, sending requests, and handling notifications. However, it is a more general-purpose library and does not include some of the higher-level abstractions found in vscode-jsonrpc.
vscode-languageserver
The vscode-languageserver package builds on top of vscode-jsonrpc to provide a framework for implementing language servers. It includes additional features and abstractions specifically for language server development, such as handling text document synchronization and providing language features like completion and hover.
node-json-rpc
The node-json-rpc package is another JSON-RPC 2.0 implementation for Node.js. It provides basic functionality for creating JSON-RPC servers and clients, sending requests, and handling notifications. It is simpler and more lightweight compared to vscode-jsonrpc, but may lack some of the advanced features and flexibility.
VSCode JSON RPC
This npm module implements the base messaging protocol spoken between a VSCode language server and a VSCode language client.
The npm module can also be used standalone to establish a JSON-RPC channel between
a client and a server. Below an example how to setup a JSON-RPC connection. First the client side.
import * as cp from 'child_process';
import * as rpc from 'vscode-jsonrpc';
let childProcess = cp.spawn(...);
let connection = rpc.createMessageConnection(
new rpc.StreamMessageReader(childProcess.stdout),
new rpc.StreamMessageWriter(childProcess.stdin));
let notification = new NotificationType<string, void>('testNotification');
connection.listen();
connection.sendNotification(notification, 'Hello World');
The server side looks very symmetrical:
import * as rpc from 'vscode-jsonrpc';
let connection = rpc.createMessageConnection(
new rpc.StreamMessageReader(process.stdin),
new rpc.StreamMessageWriter(process.stdout));
let notification = new NotificationType<string, void>('testNotification');
connection.onNotification(notification, (param: string) => {
console.log(param);
});
connection.listen();
History
3.0.0:
- converted the NPM module to use TypeScript 2.0.3.
- added strict null support.
- support for passing more than one parameter to a request or notification.
- Breaking changes:
- due to the use of TypeScript 2.0.3 and differences in d.ts generation users of the new version need to move to
TypeScript 2.0.3 as well.
License
MIT